package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderInfo;
import cn.itcast.pojo.User;
import cn.itcast.service.OrderService;
import cn.itcast.utils.UserThreadlocal;
import cn.itcast.vo.OrderInfoVo;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

/**
 * @author coolbook
 * @ClassName SubmitController
 * @Description TODO
 * @Date 15:04 2021/11/16
 * @Version 1.0
 */
@RestController
@RequestMapping("order")
@CrossOrigin
@Slf4j
public class SubmitController {
    @Reference
    private OrderService orderService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 提交预约
     * @param orderInfo
     * @return
     */
    @PostMapping("submit")
    public ResponseEntity<Result> submit(@RequestBody OrderInfo orderInfo){
      log.info("orderInfo = {}" ,orderInfo);
        //检查验证码
        //检查预定时间
        //检查人数是否已满
        //检查身份证 是否重复预约
        Boolean hasKey = this.redisTemplate.hasKey(orderInfo.getPhoneNumber());
        try {
            if (hasKey) {

                Result checkout = this.orderService.checkout(orderInfo);
                return ResponseEntity.ok(checkout);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(401).build();
    }

    /**
     * 根据预约订单号 展示订单人的订单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public  Result queryOrderInfo(@PathVariable("id") String id){
        try {
            OrderInfoVo orderInfoVo = this.orderService.queryOrderInfo(id);
            if (!ObjectUtils.isEmpty(orderInfoVo)){
                return new Result(true, MessageConstant.ORDER_SUCCESS,orderInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ORDERSETTING_FAIL);
    }
}
